//
//  HSHomeWorkController.swift
//  HomeSchoolDemo
//
//  Created by rimi on 16/9/26.
//  Copyright © 2016年 赖虹宇. All rights reserved.
//

import UIKit

class HSHomeWorkController: UITableViewController {
    
    let homeworkVM = HSHomeWorkViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //设置一个高度
        tableView.rowHeight = 100
        //注册原形cell
        tableView.register(HSHomeWorkCell.self, forCellReuseIdentifier: "homeWorkCell")
        
        loadData()
    }

    //获取数据
    func loadData(){
        
        homeworkVM.requestHomeWorkData{ [weak self](err, isReafresh) in
            
            //判断是否刷新视图
            if err != nil{
                return
            }
            if isReafresh {
                //刷新视图
                self?.tableView.reloadData()
            }
        }
        
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return homeworkVM.homeWordArr.count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "homeWorkCell", for: indexPath) as? HSHomeWorkCell
        //修改选中状态
        cell?.selectionStyle = .none
        let model = homeworkVM.homeWordArr[indexPath.row]
       //标题
        cell?.workTitle.text = model.hw_title
        //科目名
        cell?.subjectsName.setTitle(model.courseName, for: .normal)
        //教师名
        cell?.tName.text = model.hw_tName
        //发布时间
        cell?.createDate.text = model.hw_createDate
        //作业提交情况
        if homeworkVM.homeWordArr[indexPath.row].subStatus == 0 {
            cell?.subStatus.setImage(UIImage.init(named: "hw_base_commit"), for: .normal)
        }else{
            cell?.subStatus.setImage(UIImage.init(named: "hw_base_no_commit"), for: .normal)
        }
        
        return cell!
    }
    //代理方法--点击当前cell跳转页面
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //跳转到下级页面
        let detailVc = HSDetailHomeWorkController()
        present(detailVc, animated: true, completion: nil)
        print(indexPath.row)
    }
}
